// 定义单链表节点
function ListNode(val) {
    this.val = val;
    this.next = null;
}

// 创建环形链表的函数
function createCycleList(arr, pos) {
    if (arr.length === 0) return null;

    let head = new ListNode(arr[0]); // 创建头节点
    let cur = head;
    let cycleNode = null;

    for (let i = 1; i < arr.length; i++) {
        cur.next = new ListNode(arr[i]);
        cur = cur.next;
        if (i === pos) {
            cycleNode = cur; // 记录环的入口节点
        }
    }

    if (pos !== -1) {
        cur.next = cycleNode; // 形成环
    }

    return head;
}

// 示例：创建环形链表 head = [3,2,0,-4]，尾部连接到索引 1（即值 2 的节点）
let head = createCycleList([3,2,0,-4], 1);


const hasCycle = function(head){
    if(!head || !head.next) return false
    let slow= head,fast = head
    while(fast && fast.next){
        slow = slow.next
        fast = fast.next.next
        if(slow === fast){
            return true
        }
    }
    return false
}
const result = hasCycle(head)
console.log("🚀 ~ result:", result)
